package com.woniuxy.yogaapp.service.imp;

import com.woniuxy.yogaapp.mapper.AttentionMapper;
import com.woniuxy.yogaapp.mapper.ChatImageMapper;
import com.woniuxy.yogaapp.mapper.ChatMapper;
import com.woniuxy.yogaapp.pojo.Attention;
import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.ChatImage;
import com.woniuxy.yogaapp.service.InfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 进行信息处理的业务逻辑层
 */
@Service("/infoService")
public class InfoServiceImp implements InfoService {

    @Resource
    private AttentionMapper attentionMapper;
    @Resource
    private ChatMapper chatMapper;
    @Resource
    private ChatImageMapper chatImageMapper;


    /**
     * 查找关所有注对象的说说
     * @param uid
     * @return
     */
    @Override
    public List<Attention> findAllSay(Integer uid) {
        return attentionMapper.findAttentionsByUid(uid);
    }

    /**
     * 添加说说
     * @param chat
     * @return
     */
    @Override
    @Transactional
    public Integer addSay(Chat chat) {
        String result;
        //数据库中添加说说信息
        chatMapper.addChat(chat);
        return chat.getChatid();
    }

    /**
     * 从数据库中查询用户发的说说
     * @param meUid
     * @return
     */
    @Override
    public List<Chat> findUserSay(Integer meUid) {
        return chatMapper.findSayChatByUid(meUid);
    }

    /**
     * 在数据库中删除说说
     * @param chatid
     * @return
     */
    @Override
    public String deleteChat(Integer chatid) {

        String result;
        int i = chatMapper.deleteChat(chatid);
        if (i > 0) {
            result = "信息删除成功";
            return result;
        }
        result = "信息删除失败，请联系管理";
        return result;
    }

    /**
     * 在数据库中查找私聊信息
     * @param meUid
     * @param otherUid
     * @return
     */
    @Override
    public List<Chat> findPrivateChat(Integer meUid, Integer otherUid) {
        return chatMapper.findPrivateChat(meUid,otherUid);
    }

    /**
     * 动态查询通知
     * @param chat
     * @return
     */
    @Override
    public List<Chat> findChat(Chat chat) {
        return chatMapper.findChat(chat);
    }

    /**
     * 修改说说的状态
     * @param chat
     */
    @Override
    public void updateChat(Chat chat) {
        chatMapper.updateChat(chat);
    }

    /**
     * 根据给定的条件查询信息的条数
     * @param chat
     * @return
     */
    @Override
    public Integer findUnreadInfo(Chat chat) {
        return chatMapper.findUnreadInfo(chat);
    }

    /**
     * 存放图片的相对路径
     * @param chatImages
     * @return
     */
    @Override
    @Transactional
    public String addImage(List<ChatImage> chatImages) {
        String result = "图片发表成功";
        for (ChatImage chatImage : chatImages) {
            int i = chatImageMapper.addChatImage(chatImage);
            if (i <= 0) {
                result = "图片发表失败";
                return result;
            }
        }
        return result;
    }

    /**
     * 修改除私聊以外的通知
     * @param uid
     */
    @Override
    public void updateOtherChat(Integer uid) {
        chatMapper.updateOtherChat(uid);
    }

    /**
     * 修改私聊的状态未读为已读
     * @param chat
     */
    @Override
    public void updatePrivateChat(Chat chat) {
        chatMapper.updatePrivateChat(chat);
    }



    /**
     * 在数据库中查找咨询信息
     * @param meUid
     * @param otherUid
     * @return
     */
    @Override
    public List<Chat> findConsultationChat(Integer meUid, Integer otherUid) {
        return chatMapper.findConsultationChat(meUid,otherUid);
    }

    /**
     * 修改咨询的状态未读为已读
     * @param chat
     */
    @Override
    public void updateConsultationChat(Chat chat) {
        chatMapper.updateConsultationChat(chat);
    }


}
